package com.java46.admin.dessert.action;

import com.java46.admin.base.action.BaseAction;
import com.java46.admin.base.exception.BusinessException;
import com.java46.admin.dessert.dao.AdminDessertDao;
import com.java46.admin.dessert.entity.DessertAndDessertType;
import com.java46.admin.dessert.service.AdminDessertService;
import com.java46.admin.dessert.util.Page;
import com.java46.front.dessert.entity.DessertEntity;
import com.java46.front.dessertType.dao.DessertTypeDao;
import com.java46.front.dessertType.entity.DesserttypeEntity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class AdminAction extends BaseAction {

    private DessertEntity dessert;
    private String pageNO;
    private String condition;
    private String dessertId;

    private AdminDessertService adminDessertService = new AdminDessertService();
    private DessertTypeDao dessertTypeDao=new DessertTypeDao();

    public DessertEntity getDessert() {
        return dessert;
    }

    public void setDessert(DessertEntity dessert) {
        this.dessert = dessert;
    }
    public String getPageNO() {
        return pageNO;
    }

    public void setPageNO(String pageNO) {
        this.pageNO = pageNO;
    }

    public String getCondition() {
        return condition;
    }

    public void setCondition(String condition) {
        this.condition = condition;
    }
    public String getDessertId() {
        return dessertId;
    }

    public void setDessertId(String dessertId) {
        this.dessertId = dessertId;
    }

    /**
     * 查询所有商品
     * @return
     */
    @Action(value = "query_Dessert_All")
    public String query_Dessert_All(){
        HttpServletRequest request = ServletActionContext.getRequest();
        try{
            if (pageNO==null || pageNO==""){
                pageNO="1";
            }
            if (condition==null){
                condition="";
            }
            Page page = adminDessertService.query_Dessert_All(Integer.parseInt(pageNO),8,condition);
            setSuccesResult(page);
        }catch (Exception e){
            throw new BusinessException("系统异常...");
        }
        return "json";
    }





    /**
     * 删除商品
     * @return
     */
    @Action(value = "delete_Dessert",results = {
            @Result(name = "success",location = "/detail/foodList.jsp")
    })
    public String delete_Dessert(){
        if (dessertId==null || "".equals(dessertId)){
            throw new BusinessException("请传递商品id");
        }
        adminDessertService.delete_Dessert(dessertId);
        return "success";
    }

    //显示商品
    @Action(value = "query_Dessert_DessertId",results = {
            @Result(name = "success",location = "/detail/updateFood.jsp")
    })
    public String query_Dessert_DessertId(){
        DessertEntity dessert= adminDessertService.getDessertByDessertId(dessertId);
        if(dessert==null){
            throw new BusinessException("没有数据啊！");
        }
        List<DesserttypeEntity> allDessertType = dessertTypeDao.getAllDessertType();
        request.setAttribute("dessertType",allDessertType);
        request.setAttribute("dessert",dessert);
        return "success";
    }
    private String file1FileName;
    private File file1;
    public String getFile1FileName() {
        return file1FileName;
    }
    public void setFile1FileName(String file1FileName) {
        this.file1FileName = file1FileName;
    }
    public File getFile1() {
        return file1;
    }
    public void setFile1(File file1) {
        this.file1 = file1;
    }

//修改商品
    @Action(value = "update_Dessert",results = {
            @Result(name = "success",location = "/detail/foodList.jsp")
    })
    public String update_Dessert(){
        this.getFile();
        dessert.setDessertIcon(file1FileName);
        adminDessertService.update_dessert(dessert);
        return "success";
    }

    private DessertEntity dessertEntity = new DessertEntity();

    public DessertEntity getDessertEntity() {
        return dessertEntity;
    }

    public void setDessertEntity(DessertEntity dessertEntity) {
        this.dessertEntity = dessertEntity;
    }

    /**
     * 添加商品
     * @return
     */
    @Action(value = "add_Dessert",results = {
            @Result(name = "success",location = "/detail/foodList.jsp")
    })
    public String add_Dessert(){
        this.getFile();
        dessert.setDessertIcon(file1FileName);
        dessert.setDessertSum(100);
        dessert.setDessertCount(0);
        adminDessertService.add_Dessert(dessert);
        return "success";
    }
    public void getFile(){
        if (dessertEntity!=null){
            File file = new File("F:/"+file1FileName);
            try{
                // 创建文件，如果不存在则创建
                if(!file.exists()) {
                    file.createNewFile();
                }
                // 创建一个输入流读取file1变量中的数据
                FileInputStream in = new FileInputStream(file1);
                // 创建一个输出流准备给file对象输出数据
                FileOutputStream out = new FileOutputStream(file);
                // 创建一个字节数组用户存放读取的数据
                byte[] buffer = new byte[1024];
                // 每一次读取的数据在buffer中的长度
                int length = 0;
                while((length = in.read(buffer)) > 0) {
                    out.write(buffer, 0, length);
                }
                out.flush();
                out.close();
                in.close();

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
